import subprocess
import time
import config
import io
import os
import sys

def eatMacaron():
	if os.path.exists(config.miraigum.macaron.path):
		try:
			compiled = None
			with open(config.miraigum.macaron.path, 'rb') as f:
				compiled = compile(f.read(), config.miraigum.macaron.path, 'exec')
			os.rename(config.miraigum.macaron.path,
					  time.strftime('%Y%m%d_%H%M%S').join(
						  os.path.splitext(config.miraigum.macaron.path)))
			exec(compiled)
			print('好吃的马卡龙，一口一个~')
		except Exception:
			print('马卡龙的味道怎么怪怪的……', flush=True)
			pass

if __name__ == '__main__':
	sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
	while True:
		print('糖糖正在起床……', flush=True)
		eatMacaron()
		ret = subprocess.call(config.miraigum.executable, cwd=config.miraigum.cwd, encoding='utf-8', shell=True)
		print('糖糖睡着了……', flush=True)
		if ret == 0:
			break
		time.sleep(config.miraigum.sleepingSeconds)
	print('嗯~今天的任务完成了呢', flush=True)